/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.teachassistdbservice.util;

import java.text.DecimalFormat;

/**
 *
 * @author tengyu
 */
public class CommonUtil {
    public static char getCharacterNumber(int n){
        switch(n){
            case 1: return '一';
            case 2: return '二';
            case 3: return '三';
            case 4: return '四';    
            default:return '零';
        }
    }
    
    public static String formatNumber(double value,String format){
        DecimalFormat nf  = new DecimalFormat(format);
        return nf.format(value);
    }
    
    public static String toFixed(double number,int n){
        StringBuilder format = new StringBuilder("#.0");
        for(int i=1;i<n;i++){
            format.append("0");
        }
        return formatNumber(number,format.toString());
    }
    
    //格式化成5位有效数字
    public static String toFixed(double number){ 
        String result;
        if(number>10000){
            int temp = (int)number;
            int i=0;
            while(temp>100000){
                temp/=10;
                i++;
            }
            while(i>0){
                temp*=10;
                i--;
            }
            return formatNumber(temp,"#0.00");
        }else if(number>1000){
            double temp  = ((int)(number*10))/10.0;
            result = formatNumber(temp,"#0.00");
            return result;
        }else{
            result = formatNumber(number,"#0.00");
            return result;
        }
    }
    
    public static void main(String[] args){
        String s = toFixed(43232343.00523,2);
        System.out.println(s);
    }
}
